package com.cskaoyan.mall.service;

import com.cskaoyan.mall.dao.*;
import com.cskaoyan.mall.model.comment.CommentVO;
import com.cskaoyan.mall.model.goods.Spec;
import com.cskaoyan.mall.model.orders.*;
import com.cskaoyan.mall.model.user.User;
import com.cskaoyan.mall.model.comment.Comment;

import java.util.List;

/**
 * @author: jyf2000jyf
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public OrdersByPageVO orderByPage(OrdersByPageBO ordersByPageBO) {
        int total = orderDao.selectTotal(ordersByPageBO);
        List<OrdersByPageVOInnerOrderInfo> orders = orderDao.orderByPage(ordersByPageBO);
        OrdersByPageVO ordersByPageVO = new OrdersByPageVO(total, orders);
        return ordersByPageVO;
    }

    @Override
    public boolean deleteOrder(int id) {
        return orderDao.deleteOrder(id);
    }

    @Override
    public OrderByIdVO getOrderById(int orderId) {
        List<Spec> spec = orderDao.getSpecByOrderId(orderId);
        OrderByIdVO orderByIdVO = orderDao.getOrderById(orderId);
        orderByIdVO.setStates();
        orderByIdVO.setSpec(spec);
        orderByIdVO.initCurState();
        orderByIdVO.initCurSpec();
        return orderByIdVO;
    }

    @Override
    public boolean changeOrder(changeOrderBO changeOrderBO) {
        return orderDao.changeOrder(changeOrderBO);
    }

    @Override
    public List<OrderByStateVO> getOrderByState(Integer state, String username) {
        Integer UserId = userDao.getUserIdByUsername(username);
        List<OrderByStateVO> orderByStateVOList;
        if (state == -1) {
            orderByStateVOList = orderDao.getOrderByState(UserId);
        } else {
            orderByStateVOList = orderDao.getOrderByState(state, UserId);
        }
        for (OrderByStateVO orderByStateVO : orderByStateVOList) {
            OrdersByStateVOInnerGoods goods = goodsDao.getGoodsBySpec(orderByStateVO.getGoodsDetailId());
            orderByStateVO.setGoods(goods);
        }
        return orderByStateVOList;
    }

    @Override
    public boolean pay(Integer id) {
        return orderDao.pay(id);
    }

    @Override
    public boolean confirmReceive(Integer id) {
        return orderDao.confirmReceive(id);
    }

    @Override
    public boolean settleAccounts(CartList cartList) {
        return orderDao.settleAccounts(cartList);
    }

    @Override
    public boolean addOrder(AddOrderBO addOrderBO) {
        User user = userDao.getUserByUsername(addOrderBO.getToken());
        GoodsAndSpecs goodsAndSpecs = orderDao.getGoodsAndSpecsBySpecId(addOrderBO.getGoodsDetailId());
        Orders orders = new Orders(null, user.getId(), user.getNickname(), user.getRecipient(), user.getAddress(), user.getPhone(), goodsAndSpecs.getGoodsName(), goodsAndSpecs.getGoodsId(), goodsAndSpecs.getSpecName(), goodsAndSpecs.getSpecId(),
                addOrderBO.getNum(), addOrderBO.getAmount(), addOrderBO.getState(), null, null, 0);
        return orderDao.addOrder(orders);
    }

    @Override
    public boolean sendComment(CommentVO commentVO) {
        User user = userDao.getUserByUsername(commentVO.getToken());
        String specName = orderDao.getSpecNameByOrderId(commentVO.getOrderId());
        Comment comment = new Comment(null, user.getId(), user.getNickname(), commentVO.getGoodsId(), specName, commentVO.getScore(), commentVO.getContent(), null);
        return orderDao.sendComment(comment) & orderDao.setHasCommentByOrderId(commentVO.getOrderId());
    }
}
